#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/http_request.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

class hello : public cppcms::application {
public:
    hello(cppcms::service &srv) :
        cppcms::application(srv)
    {
        // dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
        //    mapper().assign("number","/number/{1}");

           dispatcher().assign("/smile",&hello::smile,this);
           mapper().assign("smile","/smile{1}");

           dispatcher().assign("",&hello::welcome,this);
           mapper().assign("");

           dispatcher().assign("/index",&hello::index,this);
           mapper().assign("/index{1}{2}");

           mapper().root("/hello");
    }
    
    virtual void smile();
    virtual void welcome();
    virtual void index();
};

void hello::index()
{
    response().out() << ":-) hehehe <br/>\n";
    response().out() << "<a href='" << url("/") << "'>Go back</a>";
    string t= request().get("t");
    std::cout << t <<endl;
    string s= request().get("s");
    std::cout << s <<endl;
}

void hello::smile()
{

    response().out() << ":-) <br/>\n";
    response().out() << "<a href='" << url("/") << "'>Go back</a>";
    string t= request().get("t");
    std::cout << t <<endl;
}

void hello::welcome()
{
    response().out() <<
        "<h1> Welcome To Page with links </h1>\n"
        "<a href='" << url("/number",1)  << "'>1</a><br>\n"
        "<a href='" << url("/number",15) << "'>15</a><br>\n"
        "<a href='" << url("/smile") << "' >:-)</a><br>\n";
    string t= request().get("t");
    std::cout << t <<endl;
}

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);

        srv.applications_pool().mount(
          cppcms::applications_factory<hello>()
        );
        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr << e.what() << std::endl;
    }
}
